package net.web.dao;

import net.web.common.utils.JdbcUtils;
import net.web.common.utils.PageInfo;
import net.web.entity.Class;

import java.util.ArrayList;
import java.util.List;

public class ClassDao {

    public PageInfo<Class> pagelist(int pageno, int pagesize, Class cls) {
        StringBuilder sql = new StringBuilder(
                "select id, name, description, parentid, coverimage, sortorder, isfeatured, createdtime, updatedtime " +
                        "from class where flag=1 "
        );
        List<Object> params = new ArrayList<>();

        if (cls.getName() != null && !cls.getName().isEmpty()) {
            sql.append(" and name like ? ");
            params.add("%" + cls.getName() + "%");
        }
        if (cls.getIsfeatured() != -1) {
            sql.append(" and isfeatured = ? ");
            params.add(cls.getIsfeatured());
        }

        sql.append(" order by isfeatured desc, sortorder desc limit ?, ? ");
        params.add((pageno - 1) * pagesize);
        params.add(pagesize);

        List<Class> list = JdbcUtils.executeQuery(Class.class, sql.toString(), params.toArray());

        StringBuilder count = new StringBuilder("select count(*) from class where flag=1 ");
        List<Object> countParams = new ArrayList<>();
        if (cls.getName() != null && !cls.getName().isEmpty()) {
            count.append(" and name like ? ");
            countParams.add("%" + cls.getName() + "%");
        }
        if (cls.getIsfeatured() != -1) {
            count.append(" and isfeatured = ? ");
            countParams.add(cls.getIsfeatured());
        }
        long total = JdbcUtils.executeQueryOneValue(count.toString(), countParams.toArray());

        return new PageInfo<>(pageno, pagesize, (int) total, list);
    }

    public List<Class> list() {
        return JdbcUtils.executeQuery(Class.class,
                "select id, name from class order by sortorder asc");
    }

    public boolean insert(Class cls) {
        String sql = "insert into class(name, description, parentid, coverimage, sortorder, isfeatured, createdtime, updatedtime) " +
                "values (?,?,?,?,?,?,?,?)";
        String now = JdbcUtils.now();
        return JdbcUtils.executeUpdate(sql,
                cls.getName(), cls.getDescription(), cls.getParentid(),
                cls.getCoverimage(), cls.getSortorder(), cls.getIsfeatured(),
                now, now) > 0;
    }

    public boolean delete(int id) {
        return JdbcUtils.executeUpdate("delete from class where id = ?", id) > 0;
    }

    public boolean update(Class cls) {
        StringBuilder sql = new StringBuilder("update class set ");
        List<Object> params = new ArrayList<>();

        if (cls.getName() != null && !cls.getName().isEmpty()) {
            sql.append("name = ?, "); params.add(cls.getName());
        }
        if (cls.getDescription() != null) {
            sql.append("description = ?, "); params.add(cls.getDescription());
        }
        if (cls.getParentid() != 0) {
            sql.append("parentid = ?, "); params.add(cls.getParentid());
        }
        if (cls.getCoverimage() != null && !cls.getCoverimage().isEmpty()) {
            sql.append("coverimage = ?, "); params.add(cls.getCoverimage());
        }
        if (cls.getSortorder() != 0) {
            sql.append("sortorder = ?, "); params.add(cls.getSortorder());
        }
        if (cls.getIsfeatured() != -1) {
            sql.append("isfeatured = ?, "); params.add(cls.getIsfeatured());
        }

        if (params.isEmpty()) return false;

        sql.deleteCharAt(sql.lastIndexOf(","));
        sql.append(", updatedtime = ? where id = ?");
        params.add(JdbcUtils.now());
        params.add(cls.getId());

        return JdbcUtils.executeUpdate(sql.toString(), params.toArray()) > 0;
    }
}